<template>
  <div class="app-container">
    <div class="filter-container">
      <el-form :inline="true" :model="formModel">
        <el-form-item class="no-warn-item" label="工厂:" prop="plantNo">
          <el-select v-model="formModel.plantNo" clearable filterable>
            <el-option v-for="item in plantOptions" :key="item.tmBasPlantId" :label="item.plantNameC" :value="item.plantNo"></el-option>
          </el-select>
        </el-form-item>
        <el-form-item class="no-warn-item" label="批次号:" prop="batchNo">
          <el-input v-model="formModel.batchNo" disabled>
            <el-button slot="append" type="primary" icon="el-icon-search" @click="queryBatchPlan"></el-button>
          </el-input>
        </el-form-item>
        <el-form-item class="no-warn-item" label="计划编号:" prop="planNo">
          <el-input
            v-model="formModel.planNo"
            class="filter-item "
            clearable
          />
        </el-form-item>
        <el-form-item class="no-warn-item" label="物料编码:" prop="materialNo">
          <el-input v-model="formModel.materialNo" disabled>
            <el-button slot="append" type="primary" icon="el-icon-search" @click="queryMaterialData"></el-button>
          </el-input>
        </el-form-item>
        <el-form-item class="no-warn-item" label="计划状态:" prop="planStatus">
          <hey-select v-model="formModel.planStatus" clcd="plan_status_cd" />
        </el-form-item>
        <el-row class="submit-area">
          <el-col :span="24">
            <el-button type="primary" v-if="hasPermission('product_plan_select')" plain @click="queryList(false)">查询</el-button>
            <el-button type="primary" v-if="hasPermission('product_plan_clear')" plain @click="clearCondition">清除</el-button>
            <el-button type="primary" v-if="hasPermission('product_plan_add')" plain @click="handleAdd">新增</el-button>
            <el-button type="primary" v-if="hasPermission('erp_data_receive')" plain @click="handleErp">ERP数据接收</el-button>
          </el-col>
        </el-row>
      </el-form>
    </div>
    <hey-table ref="table" :table-data="tableData"  :columns="columns">
      <template slot="tools">
        <el-table-column
          label="操作"
          width="270"
          fixed="right"
          header-align="center"
          align="center"
        >
          <template slot-scope="{row}">
            <span class="hey_tools_btn" v-if="hasPermission('product_plan_edit')" @click="handleEdit(row)">编辑</span>
            <span class="hey_tools_btn" v-if="hasPermission('product_plan_resolve')" @click="handleResolve(row)">分解</span>
            <span class="hey_tools_btn" v-if="hasPermission('product_plan_match')" @click="handleMatch(row)">匹配</span>
            <span class="hey_tools_btn" v-if="hasPermission('product_plan_del')" @click="handleDel(row)">删除</span>
            <span class="hey_tools_btn" v-if="hasPermission('product_plan_hold')" @click="handleHold(row)">冻结</span>
            <span class="hey_tools_btn" v-if="hasPermission('product_plan_remove_hold')" @click="handleRemoveHold(row)">解冻</span>
          </template>
        </el-table-column>
      </template>
    </hey-table>
    <el-dialog
      v-dialogDrag
      v-if="dialogVisible"
      :close-on-click-modal="false"
      title="生产计划信息"
      :visible.sync="dialogVisible"
      width="80%"
    >
      <product-plan-mnt :page-type="operType" :edit-data="editData" @mntSuc="queryList" />
    </el-dialog>
    <el-dialog
      v-dialogDrag
      v-if="showBatchPlan"
      :close-on-click-modal="false"
      title="批次计划信息"
      :visible.sync="showBatchPlan"
      width="80%"
      append-to-body
    >
      <batch-plan-dialog @queryBatchPlanSuc="queryBatchPlanSuc" />
    </el-dialog>
    <el-dialog
      v-dialogDrag
      v-if="showMaterialData"
      :close-on-click-modal="false"
      title="物料主数据"
      :visible.sync="showMaterialData"
      width="90%"
      append-to-body
    >
      <material-data-dialog @queryMaterialDataSuc="queryMaterialDataSuc" />
    </el-dialog>
    <el-dialog
      v-dialogDrag
      v-if="showHoldAndDel"
      :close-on-click-modal="false"
      :title="dialogTitle"
      :visible.sync="showHoldAndDel"
      width="50%"
      append-to-body
    >
      <product-plan-hold-and-del :page-type="operType" :edit-data="editData" @mntSuc="queryList" />
    </el-dialog>
    <el-dialog
      v-if="showOrderMatch"
      v-dialogDrag
      :close-on-click-modal="false"
      title="订单匹配信息"
      :visible.sync="showOrderMatch"
      width="60%"
    >
      <order-match-mnt :edit-data="editData" @mntSuc="queryList" />
    </el-dialog>
  </div>
</template>

<script>

  export default {
    name: 'ProductPlanList',
    components: {
      MaterialDataDialog: () => import('@/views/dialog/material-data-dialog'),
      BatchPlanDialog: () => import('@/views/dialog/batch-plan-dialog'),
      ProductPlanMnt: () => import('./product-plan-mnt'),
      ProductPlanHoldAndDel: () => import('./product-plan-hold-and-del'),
      OrderMatchMnt: () => import('./order-match-mnt'),
    },
    props: {},
    data() {
      return {
        dialogVisible: false,
        showOrderMatch: false,
        showMaterialData:false,
        showBatchPlan:false,
        showHoldAndDel:false,
        operType: '',
        dialogTitle: '',
        editData:{},
        tableData:[],
        plantOptions:[],
        formModel: {
          plantNo: '',
          batchNo: '',
          planNo: '',
          materialNo: '',
          planStatus: '',
          isDelete: '0'
        },
        columns: [
          { title: '计划编号', key: 'planNo'},
          { title: '批次号', key: 'batchNo'},
          { title: 'ERP计划编号', key: 'erpOrderNo'},
          { title: '计划状态', key: 'planStatus',clcd:'plan_status_cd'},
          { title: '工厂代码', key: 'plantNo'},
          { title: '工厂名称', key: 'plantNameC'},
          { title: '是否试制', key: 'isTrialProduct',clcd:'ind_cd'},
          { title: '物料编码', key: 'materialNo'},
          { title: '物料描述', key: 'materialDesc'},
          { title: '车型', key: 'productTypeKey',clcd:'key_product_type'},
          { title: '生产类型', key: 'proType',clcd:'Key_Product_Type'},
          { title: '销售类型', key: 'saleType',clcd:'Key_Sale_Type'},
          { title: '需求类型', key: 'needType',clcd:'need_type_cd'},
          { title: 'VIN年份', key: 'vinYear',clcd:'Key_Vin_ModeYear'},
          { title: '需求日期', key: 'needDate'},
          { title: '计划数量', key: 'qty'},
          { title: '冻结理由', key: 'holdReasonDesc'},
          { title: '删除理由', key: 'deleteReasonDesc'},
          { title: '创建时间', key: 'createTime' },
          { title: '创建人', key: 'createUsername' },
          { title: '更新次数', key: 'updateNum'},
          { title: '最后修改时间', key: 'lastUpdateTime'},
          { title: '最后修改人', key: 'lastUpdateUsername' },
        ]
      }
    },
    computed: {

    },
    mounted() {
      this.getPlants()
    },
    methods: {
      queryMaterialData(){
        this.showMaterialData = true
      },
      queryMaterialDataSuc(data){
        this.formModel.materialNo = data.materialNo
      },
      queryBatchPlan(){
        this.showBatchPlan = true
      },
      queryBatchPlanSuc(data){
        this.formModel.batchNo = data.batchNo
      },
      clearCondition(){
        this.resetObj(this.formModel)
        this.formModel.isDelete = '0'
      },
      getPlants(){
        const param = {pageNum:1,pageSize:9999}
        param.isDelete = '0'
        param.status = '1'
        this.get('/tmBasPlant/queryTmBasPlant', param).then(res => {
          this.plantOptions = res.list
        })
      },
      queryList() {
        this.$refs.table.query('/teOfmVehiclePlan/queryTeOfmVehiclePlan', this.formModel, '', false)
      },
      handleAdd() {
        this.operType = 'add'
        this.dialogVisible = true
      },
      handleEdit(row) {
        if(row.planStatus !='1'){
          this.$warn('只有计划状态为未分解的数据才能进行该操作!')
          return false
        }
        this.operType = 'edit'
        this.editData = row || {}
        this.dialogVisible = true
      },
      handleMatch(row) {
        console.log('row',row)
        if (row.planStatus != '2') {
          this.$warn('只有计划状态为已分解的数据才能进行该操作!')
          return false
        }
        // 通过工厂代码 车型 销售类型 生产类型匹配工艺路线
        const param = {}
        param.plantNo = row.plantNo
        param.productTypeKey = row.productTypeKey
        param.saleType = row.saleType
        param.proType = row.proType
        this.get('/tmOfmOrder/orderMatchTmBasRoute', param).then(res => {
          if (!res || res.length == 0) {
            this.$warn('该计划工厂下没有与之匹配的车型.生产类型,销售类型的工艺路线!')
            return false
          } else {
            row.routeOptions = res
            this.editData = row || {}
            // 打开匹配页面
            this.showOrderMatch = true
          }
        })
      },
      handleResolve(row){
        if(row.planStatus !='1'){
          this.$warn('只有计划状态为未分解的数据才能进行该操作!')
          return false
        }
        const param = row
        param.operateType = '10'
        param.createTime = ''
        param.lastUpdateTime = ''
        const url = '/teOfmVehiclePlan/maintainTeOfmVehiclePlan'
        const tip = '计划分解成功!'
        this.post(url, param).then(res => {
          this.$info(tip, () => {
            this.queryList()
          })
        })
      },
      handleHold(row){
        if(row.planStatus !='1'){
          this.$warn('只有计划状态为未分解的数据才能进行该操作!')
          return false
        }
        this.dialogTitle = '计划冻结'
        this.operType = 'hold'
        this.editData = row || {}
        this.showHoldAndDel = true
      },
      handleDel(row){
        if(!(row.planStatus =='1' || row.planStatus =='3')){
          this.$warn('只有计划状态为HOLD和未分解的数据才能进行该操作!')
          return false
        }
        this.dialogTitle = '计划删除'
        this.operType = 'del'
        this.editData = row || {}
        this.showHoldAndDel = true
      },
      handleErp(){
        this.$info('ERP数据接收完毕,接收数据成功N条,失败M条!')
      },
      handleRemoveHold(row){
        if(row.planStatus !='3'){
          this.$warn('只有计划状态为HOLD的数据才能进行该操作!')
          return false
        }
        const param = {}
        param.operateType = '12'
        param.rcrdId = row.rcrdId
        param.planNo = row.planNo
        const url = '/teOfmVehiclePlan/maintainTeOfmVehiclePlan'
        const tip = '解除冻结成功!'
        this.post(url, param).then(res => {
          this.$info(tip, () => {
            this.queryList()
          })
        })
      },
    }
  }
</script>


